package com.sunmouse.toollib.widget;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunmouse.toollib.R;

/**
 * Created by Kvin on 2017/5/18.
 */
public class LoadingDialog extends AlertDialog {
    private View mContentView;
    private Context mContext;
    private ProgressBar animBar;
    private TextView nameTv;

    private Animation animation;


    public static LoadingDialog mDialog;

    private LoadingDialog(Context context) {
        super(context);
    }

    private LoadingDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    private LoadingDialog(Context context, int themeResId) {
        super(context, themeResId);

        init(context);
    }

    private void init(Context context) {
        mContentView = View.inflate(context, R.layout.dialog_loading, null);
        animBar = (ProgressBar) mContentView.findViewById(R.id.anim_bar);
        animBar.setIndeterminate(true);
        animBar.setIndeterminateDrawable(context.getResources().getDrawable(R.mipmap.default_loading_rotate));

        nameTv = (TextView) mContentView.findViewById(R.id.title_tv);

        animation = AnimationUtils.loadAnimation(context, R.anim.loading_anim_rotate);
        animation.setFillAfter(true);

        mContext = context;
    }

    /**
     *
     *
     */
    public static LoadingDialog getLoadingDialog(Context context) {
        if (mDialog == null) {
            mDialog = new LoadingDialog(context, R.style.DialogUtil);
        }
        return mDialog;
    }

    /**
     * create a new dialog
     *
     * @param context
     * @return
     */
    public static LoadingDialog newLoadingDialog(Context context) {
        return new LoadingDialog(context, R.style.DialogUtil);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setWindowAnimations(R.style.Transition_FadeInFadeOut);//set loading animation
        //set dialog size
//        DisplayMetrics metrics = new DisplayMetrics();
//        getWindow().getWindowManager().getDefaultDisplay().getMetrics(metrics);
//        WindowManager.LayoutParams params = new WindowManager.LayoutParams();
//        params.width = (int) (metrics.widthPixels * 0.5);
//        params.height = (int) (metrics.heightPixels * 0.2);
//        getWindow().setAttributes(params);
        if (mContentView != null)
            setContentView(mContentView);
    }

    @Override
    public void show() {
        if (!isShowing()) {
            super.show();
            animBar.startAnimation(animation);
        }
    }


    @Override
    public void dismiss() {
        if (isShowing()) {
            animBar.clearAnimation();
            super.dismiss();
        }
    }


    /**
     * set loading image
     */
    public void setLoadingImage(@DrawableRes int resId) {
        if (animBar != null && mContext != null) {
            animBar.setIndeterminateDrawable(mContext.getResources().getDrawable(resId));

        }
    }

    /**
     * set dialog bg
     */
    public void setDialogBackground(@ColorRes int color) {
        if (mContentView != null) {
            mContentView.setBackgroundResource(color);
        }
    }

    /**
     * set scales of dialog
     */
    public void setDialogScale(int scale) {
        if (animBar == null || nameTv == null) return;
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(scale, scale);
        animBar.setLayoutParams(params);
        nameTv.setLayoutParams(params);
    }

    /**
     * set loading text
     */
    public void setLoadingText(String text) {
        if (nameTv == null) return;
        if (TextUtils.isEmpty(text)) {
            nameTv.setText("");
            return;
        }
        nameTv.setText(text);

    }

    /**
     * set loading text size
     */
    public void setLoadingTextSize(float size) {
        if (nameTv == null) return;
        nameTv.setTextSize(size);
    }

    /**
     * set layout params
     */
    public void setLayoutParams(int w, int h) {
        if (mContentView != null) {
            mContentView.setLayoutParams(new ViewGroup.LayoutParams(w, h));
        }
    }

    public void setLayoutParams(ViewGroup.LayoutParams lp) {
        if (mContentView != null) {
            mContentView.setLayoutParams(lp);
        }
    }
}
